var express = require('express');//创建路由
//连接池导出了一个Promise构造函数的方法有两个参数第一个参数sql语句，第二个参数是把第一个参数的问号补齐1代表短信验证
var query = require('../util/dbHelper.js');//引入连接池，
var router = express.Router();
router.post("/messageLogin",function(req,res,next){
    let phone = req.body.phone,//取出
        code = req.body.code,
        userinfo = req.body.userinfo;
    if(code === "6666"){
        req.session.userinfo = userinfo;//写入session
        req.myPromise =
            query("CALL p_mi_login(1,?,?);",[phone,""])
                .then(result => {
                   return result[0][0].reset
                });
    }else{
        req.myPromise = new Promise((resolve,reject) => {//成功和失败
            resolve("验证码错误");
        });
    }
    next();
});
router.post("/passwordLogin",function(req,res,next){
    let name = req.body.name,//取出mi文件项目中ajax提交的name和pwd
        pwd = req.body.pwd,
        userinfo = req.body.userinfo;
    req.myPromise =  //在req里自定义一个myPromise是Promise构造函数
        query("CALL p_mi_login(0,?,?);",[name,pwd])
            .then(result => {
                //在这里判断成功和失败（账号不对密码错误等）
                if(!result[0][0].result){
                    req.session.userinfo = userinfo;//写入session
                }else{}
                return result[0][0].result
            });
    next();
});

module.exports = router;